#include <boost/test/test_tools.hpp>

#include <iterator>
#include <sstream>
#include <iostream>
#include <string>

#include "../src/PlanFileDao.hpp"
#include "../src/Plan.hpp"

using namespace boost;
using namespace fixstoker;
using namespace std;

void testPerfRunFileDao() {
	// GIVEN
	BOOST_TEST_MESSAGE("testPerfRunFileDao");
	PlanFileDao dao;
	istringstream
			input(
					"template:AAA:tag1=value1|tag2=value2|\n\
template:BBB:tag1=value1|tag2=value2|\n\
template:CCC:tag1=value1|tag2=value2|\n\
memcpy:AAA:10,20,valueAAA\n\
memcpy:BBB:10,20,valueBBB\n\
memcpy:CCC:10,20,valueCCC1:30,40,valueCCC2\n\
memcpy:BBB:11,21,valueBBB11:31,41,valueBBB22\n");

	// WHEN
	const Plan* plan = dao.load(input);

	// THEN
	BOOST_CHECK_EQUAL(3, plan->numberOfMessageTemplates());

	vector<const PlanEntry*> entries = plan->entries();
	BOOST_CHECK_EQUAL(4, entries.size());

	{
		// memcpy:AAA:10,20,valueAAA
		const PlanEntry* entry = entries[0];
		BOOST_CHECK_EQUAL(0, entry->msgTemplateIndex());
		BOOST_CHECK_EQUAL(1, entry->substitutions().size());
		BOOST_CHECK_EQUAL(10, entry->substitutions()[0]->from());
		BOOST_CHECK_EQUAL(20, entry->substitutions()[0]->to());
		BOOST_CHECK_EQUAL("valueAAA", entry->substitutions()[0]->value());
	}
	{
		// memcpy:BBB:10,20,valueBBB
		const PlanEntry* entry = entries[1];
		BOOST_CHECK_EQUAL(1, entry->msgTemplateIndex());
		BOOST_CHECK_EQUAL(1, entry->substitutions().size());
		BOOST_CHECK_EQUAL(10, entry->substitutions()[0]->from());
		BOOST_CHECK_EQUAL(20, entry->substitutions()[0]->to());
		BOOST_CHECK_EQUAL("valueBBB", entry->substitutions()[0]->value());
	}
	{
		// memcpy:CCC:10,20,valueCCC1:30,40,valueCCC2
		const PlanEntry* entry = entries[2];
		BOOST_CHECK_EQUAL(2, entry->msgTemplateIndex());
		BOOST_CHECK_EQUAL(2, entry->substitutions().size());
		BOOST_CHECK_EQUAL(10, entry->substitutions()[0]->from());
		BOOST_CHECK_EQUAL(20, entry->substitutions()[0]->to());
		BOOST_CHECK_EQUAL("valueCCC1", entry->substitutions()[0]->value());
		BOOST_CHECK_EQUAL(30, entry->substitutions()[1]->from());
		BOOST_CHECK_EQUAL(40, entry->substitutions()[1]->to());
		BOOST_CHECK_EQUAL("valueCCC2", entry->substitutions()[1]->value());
	}
	{
		// memcpy:BBB:11,21,valueBBB11:31,41,valueBBB22
		const PlanEntry* entry = entries[3];
		BOOST_CHECK_EQUAL(1, entry->msgTemplateIndex());
		BOOST_CHECK_EQUAL(2, entry->substitutions().size());
		BOOST_CHECK_EQUAL(11, entry->substitutions()[0]->from());
		BOOST_CHECK_EQUAL(21, entry->substitutions()[0]->to());
		BOOST_CHECK_EQUAL("valueBBB11", entry->substitutions()[0]->value());
		BOOST_CHECK_EQUAL(31, entry->substitutions()[1]->from());
		BOOST_CHECK_EQUAL(41, entry->substitutions()[1]->to());
		BOOST_CHECK_EQUAL("valueBBB22", entry->substitutions()[1]->value());
	}

}

